package com.github.cdisk.xtrans.ui.toolbox;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.github.cdisk.xtrans.connect.Connect;

public class ConnectDialog extends Dialog {

	protected Connect result;
	protected Shell shell;
	private Text text_name;
	private Text text_pass;
	private Text text_user;
	private Text text_url;
	private Text text_driver;
	private Text text_charset;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public ConnectDialog(Shell parent, int style) {
		super(parent, style);
		setText("编辑连接");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Connect open(Connect connect) {
		result = connect;
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 326);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(10, 13, 72, 17);
		lblNewLabel.setText("连接名称：");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(10, 50, 72, 17);
		lblNewLabel_1.setText("驱动类：");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(10, 85, 72, 17);
		lblNewLabel_2.setText("连接字符串：");
		
		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setBounds(10, 120, 72, 17);
		lblNewLabel_3.setText("账号：");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(10, 158, 72, 17);
		label.setText("密码：");

		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("字符集：");
		label_1.setBounds(10, 191, 72, 17);
		
		
		text_name = new Text(shell, SWT.BORDER);
		text_name.setBounds(88, 10, 346, 23);

		text_driver = new Text(shell, SWT.BORDER);
		text_driver.setBounds(88, 47, 346, 23);

		text_url = new Text(shell, SWT.BORDER);
		text_url.setBounds(88, 82, 346, 23);
		
		text_user = new Text(shell, SWT.BORDER);
		text_user.setBounds(88, 117, 346, 23);
		
		text_pass = new Text(shell, SWT.BORDER);
		text_pass.setBounds(88, 155, 346, 23);

		text_charset = new Text(shell, SWT.BORDER);
		text_charset.setBounds(88, 188, 346, 23);
		
		/* 非空的情况下就赋值，说明是更新操作。 */
		if (result != null) {
			text_name.setText(StringUtils.trimToEmpty(result.getId())); 
			text_driver.setText(StringUtils.trimToEmpty(result.getDriver()));
			text_url.setText(StringUtils.trimToEmpty(result.getUrl()));
			text_user.setText(StringUtils.trimToEmpty(result.getUser()));
			text_pass.setText(StringUtils.trimToEmpty(result.getPassword()));
			text_charset.setText(StringUtils.trimToEmpty(result.getCharset()));
		}
		
		Button btnc = new Button(shell, SWT.NONE);
		btnc.setBounds(354, 260, 80, 27);
		btnc.setText("取消(&C)");
		btnc.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		
		Button btno = new Button(shell, SWT.NONE);
		btno.setBounds(268, 260, 80, 27);
		btno.setText("确定(&O)");
		btno.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (result == null) {
					result = new Connect();
				}
				result.setId(text_name.getText());
				result.setDriver(text_driver.getText());
				result.setUrl(text_url.getText());
				result.setUser(text_user.getText());
				result.setPassword(text_pass.getText());
				result.setCharset(text_charset.getText());
				shell.close();
			}
		});

	}
}
